﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Data.Model;
using NVelocity.App;
using NVelocity.Runtime;
using NVelocity;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.Temp
{
    public partial class Default : UrlRewritePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            //获取参数
            int temp_page_id = 0, docid = 0, default_page_id = 0;
            string code = Request["account"];
            if(!string.IsNullOrEmpty(code))
            {
                using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    int tid = session.CreateQuery("select Id from CmsUserTemplateInfo where OwnerAccount=:code and Status='1'")
                        .SetParameter("code", code)
                        .UniqueResult<int>();
                    default_page_id = session.CreateQuery("select Id from CmsUserTemplatePage where PageLevel='1' and UserTempId=:tid")
                        .SetMaxResults(1)
                        .SetParameter("tid", tid)
                        .UniqueResult<int>();
                }
            }
            if (default_page_id == 0)
            {
                Response.Write("参数错误");
                Response.End();
                return;
            }

            try
            {
                temp_page_id = Int32.Parse(Request["tempid"]);
            }
            catch
            {

            }
            if (temp_page_id == 0)
            {
                temp_page_id = default_page_id;
            }

            try
            {
                docid = Int32.Parse(Request["docid"]);
            }
            catch 
            {
            	
            }


            //输出html
            VelocityEngine vltEngine = new VelocityEngine();
            vltEngine.Init();

            VelocityContext vltContext = new VelocityContext();
            System.IO.StringWriter vltWriter = new System.IO.StringWriter();

            CmsUserTemplatePage templatePage = null;
            using(ISession session = SessionFactoryHelper.CreateSession())
            {
                templatePage = session.Get<CmsUserTemplatePage>(temp_page_id);
                
                string tempdata = "";
                if (templatePage.Kind == "list")
                {
                    //展示明细页面
                    if (docid > 0)
                    {
                        //获取当前页面的下级页面
                        CmsUserTemplatePage nextTemplatePage = session.Get<CmsUserTemplatePage>(templatePage.NextUtpId);
                        tempdata = nextTemplatePage.TemplateData;

                        CmsDocInfo doc = session.Get<CmsDocInfo>(docid);
                        if (doc == null)
                        {
                            doc = new CmsDocInfo();
                        }
                        vltContext.Put("doc", doc);
                    }
                    //展示列表页面
                    else
                    {
                        tempdata = templatePage.TemplateData;

                        IList<CmsDocInfo> docs = session.CreateQuery("select a from CmsDocInfo a, CmsUserTemplateDoc b where a.Id=b.DocId and b.UserTemplatePagesId=:tempid and a.OwnerAccount=:acc")
                            .SetParameter("tempid", temp_page_id)
                            .SetParameter("acc", code)
                            .List<CmsDocInfo>();

                        vltContext.Put("docs", docs);
                    }
                }
                //展示页面
                else
                {
                    CmsDocInfo doc =
                        session.CreateQuery("select a from CmsDocInfo a, CmsUserTemplateDoc b where a.Id=b.DocId and b.UserTemplatePagesId=:tempid")
                        .SetParameter("tempid", temp_page_id)
                        .UniqueResult<CmsDocInfo>();
                    if (doc == null)
                    {
                        doc = new CmsDocInfo();
                    }
                    vltContext.Put("doc", doc);

                    tempdata = templatePage.TemplateData;
                }

                //获取所有二级页面
                IList<CmsUserTemplatePage> lists = session.CreateQuery("from CmsUserTemplatePage where UserTempId=:tempid and PageLevel='2' order by Sort")
                    .SetParameter("tempid", templatePage.UserTempId)
                    .List<CmsUserTemplatePage>();
                CmsUserTemplateInfo tempObj =session.Get<CmsUserTemplateInfo>(templatePage.UserTempId);
                
                //模板信息
                vltContext.Put("temp", tempObj);

                //所有栏目
                vltContext.Put("cols", lists);

                //当前栏目
                vltContext.Put("col", templatePage);

                //文件工具
                vltContext.Put("FileKit", new FileKitWrapper());

                //参数工具
                vltContext.Put("TemplateParamUtil", new TemplateParamUtil(tempObj.Params));

                //页面链接工具
                vltContext.Put("TemplatePageUtil", new TemplatePageUtil(code));

                //首页
                vltContext.Put("default_col", session.CreateQuery("from CmsUserTemplatePage where UserTempId=:tempid and PageLevel='1'")
                    .SetParameter("tempid", templatePage.UserTempId)
                    .SetMaxResults(1)
                    .UniqueResult<CmsUserTemplatePage>());

                vltEngine.Evaluate(vltContext, vltWriter, null, tempdata);
                Response.Write(vltWriter.GetStringBuilder().ToString());
            }
        }
    }
}